﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Presenter.Support;
using Microsoft.SharePoint.Taxonomy;
using MS.InformationManagement.Support;
using MS.InformationManagement.BLL.TaxonomyTerms;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.Presenter.TermsSelector
{
    public class TermSelectorPresenter : BasePresenter<ITermSelectorView>
    {

        #region Constructors

        public TermSelectorPresenter(ITermSelectorView view) : base(view) { }

        #endregion

        #region Methods

        public void HandleLoad()
        {
            TaxonomyTerms bllObject = new TaxonomyTerms();
            View.Countries = bllObject.GetCountries();
            if (View.SelectedTerm != null)
            { 
                Term selectedState = TermStoreHelper.GetParentTerm(View.SelectedTerm,GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_COUNTRY);
                Term selectedCity = TermStoreHelper.GetTermByTaxonomyValue(View.SelectedTerm,GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_COUNTRY);
                Term selectedCountry = selectedState.Parent;
                View.Cities = selectedState.Terms;
                View.States = selectedCountry.Terms;
                View.SelectedCountry = selectedCountry.Labels[0].Value;
                View.SelectedState = selectedState.Labels[0].Value;
            }

            View.Show();
        }

        public void LoadStates()
        {
            View.States = new TaxonomyTerms().GetStates(View.SelectedCountry);
            View.ShowStates();
        }

        public void LoadCities()
        {
            View.Cities = new TaxonomyTerms().GetCities(View.SelectedCountry, View.SelectedState);
            View.ShowCities();
        }

        

        #endregion

    }
}
